<template>
  <div class="login-setting">
    <el-form
      ref="userForm"
      :model="userInfo"
      :rules="rules"
      label-width="120px"
      style="margin-left: 120px; margin-top: 30px"
    >
      <el-form-item label="姓名:">
        <el-input
          v-model="userInfo.username"
          style="width: 300px"
          prop="username"
        />
      </el-form-item>

      <el-form-item label="密码:">
        <el-input
          v-model="userInfo.password"
          type="password"
          style="width: 300px"
          prop="password"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="saveUserDetailById">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getUserDetailByIdAPI, saveUserDetailByIdAPI } from '@/api/user' // 导入API方法
export default {
  name: 'LoginSetting',
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', triggler: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', triggler: 'blur' }
        ]
      }
    }
  },
  created() {
    // 调用获取员工信息的方法
    this.getUserDetailById()
  },
  methods: {
    // 获取员工基本信息
    async getUserDetailById() {
      // 调用获取员工信息的API方法
      const res = await getUserDetailByIdAPI(this.$route.query.id)
      // 将返回的数据赋值给userInfo
      this.userInfo = res.data
      console.log(res.data)
    },

    // 更新员工信息
    saveUserDetailById() {
      // 兜底验证
      this.$refs.userForm.validate(async(valid) => {
        if (!valid) return
        // 调用更新员工信息的API方法
        const res = await saveUserDetailByIdAPI(this.userInfo)
        // 重新调用获取员工信息的方法
        this.getUserDetailById()
        // 提示成功
        this.$message.success(res.message)
        console.log(res)
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
